package com.kitty.game.pet.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.utils.Const;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component("UseDanHandler")
public class UseDanHandler extends FeedPetHandler {
    private static final List<String> items = Arrays.asList("超级神兽丹", "神兽丹");
    private static final String ERROR_ITEM_MESSAGE = "请放入正确的道具。";
    private static final String SUCCESS_MESSAGE = "#R{0}#n寿命成功补充#R{1}#n点，并增加#R{2}#n点亲密度。";

    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        if (!items.contains(roleEquip.getName())) {
            return ERROR_ITEM_MESSAGE;
        }
        if (roleEquip.queryNum() <= 0) {
            return MessageFormat.format(Const.ROLE_RESOURCE_NOT_ENOUGH, roleEquip.getName());
        }
        equipService.delEquipNum(role, roleEquip, 1);
        if (roleEquip.isLimit()) {
            petService.addLimitTime(pet, 1);
        }

        int addIntimacy = 0;
        int addLongevity = 0;
        int longevity = pet.getLongevity();
        if ("神兽丹".equals(roleEquip.getName())) {
            addLongevity = 500;
        } else if ("超级神兽丹".equals(roleEquip.getName())) {
            addLongevity = 5000;
            addIntimacy = 2000;
            pet.setIntimacy(pet.getIntimacy() + addIntimacy);
        }
        if (longevity + addLongevity >= 15000) {
            longevity = 15000;
            addLongevity = 0;
        } else {
            longevity += addLongevity;
        }
        pet.setLongevity(longevity);
        role.save();
        //面板属性
        RespUpdate respUpdate = new RespUpdate();
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(61, pet.getLongevity()));
        list.add(new FiedValue(270, pet.getLimitTime()));
        list.add(new FiedValue(63, pet.getIntimacy()));
        list.add(new FiedValue(923, pet.getIntimacy()));
        respUpdate.setRoleId(pet.getId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);
        return MessageFormat.format(SUCCESS_MESSAGE, pet.getName(), String.valueOf(addLongevity), String.valueOf(addIntimacy));
    }
}
